/*PersonProfile control class*/
$N.ctrl.PersonProfileCtrl = (function() {
    var notify;
    var personProfileArray = new Array();
    var selectedIndex = 0;

    function _sqlResponseCb(tx, result) {
        console.log("personProfileArray.length = " + personProfileArray.length);
        if (result.rowsAffected != 0) {
            console.log("sql operation successfully!!!");
        } else {
            console.log("no person is managed!");
        }
    }

		function _deletePersonProfileSuccessCb() {
			console.log("delete the personProfile successfully!");
		}
		
		function _deletePersonProfileFailCb() {
			console.log("delete the personProfile failed!");
		}
    
    function _ajaxUploadResponseCb(jsonResponse) {
        console.log(jsonResponse);
        var data = personProfileArray[selectedIndex];
        if (jsonResponse.length === 0) {
            console.log("json_response string is empty");
        } else {
            if (jsonResponse.search("OK") !== -1) {
            	$N.databaseSet.PersonProfileTable.deletePersonProfile(data.personID, _deletePersonProfileSuccessCb, _deletePersonProfileFailCb);
            	$N.databaseSet.PersonProfileTable.addPersonProfile(data, _sqlResponseCb, null );
            } 
        } 
        return;
    }

    function _ajaxResponseCb(jsonResponse) {
        console.log(jsonResponse);
        if (jsonResponse.length == 0) {
            //put a placeholder
            console.log("json_response string is empty.");
        } else {
            if (jsonResponse.search("NO MORE") != -1 || jsonResponse.search("FAILED") !== -1) {
                return;
            }
            jsonObject = JSON.parse(jsonResponse);
            //save data into the database
            for (var i = 0; i < jsonObject.length; i++) {
                var data = $.extend({}, jsonObject[i].PersonProfile);
                personProfileArray.push(data);
                $N.databaseSet.PersonProfileTable.addPersonProfile(data);
                notify(data);
            }
        }
    }

    function getPersonProfilesuccessCb(tx, result, data) {
        console.log("get data successfully" + result);
        if (result.rows.length != 0) {
        	var data = $.extend({},result.rows.item(0));
        	console.log("data = " + data);
            personProfileArray.push(data);
            notify(data);
        } else { /*nothing in local db*/
            console.log("getPersonProfile : personID = " + data);
            downloadPersonProfileData(data);
        }
    }

    function insertDataIntoCore(data) {
        personProfileArray.push(data);
        $N.databaseSet.PersonProfileTable.addPersonProfile(data, _sqlResponseCb);
    }

    function downloadPersonProfileData(personID) {
        var postURL = $N.util.getPersonProfileDownloadURL();
        var para = {
            jsonstring: JSON.stringify({
                personID: personID
            })
        };
        $N.util.ajaxPost(postURL, para, _ajaxResponseCb);
    }

    function uploadPersonProfileData(data) {
        var postURL = $N.util.getPersonProfileUploadURL();
        personProfileArray[selectedIndex] = data;
        
        var para = {
            jsonstring: JSON.stringify(data)
        };
        console.log("personProfile = " + data);
        $N.util.ajaxPost(postURL, para, _ajaxUploadResponseCb);
    }

    function getPersonProfileData(data) {
        var callbackObj = new Callback(data.personID, getPersonProfilesuccessCb);
        $N.databaseSet.PersonProfileTable.getPersonProfile(data.personID, callbackObj);
    }

    function setSelectedData(index) {
    		console.log("setter : this->selectedIndex = " + index);
        selectedIndex = index;
    }

    function getSelectedData() {
        console.log("this->selectedIndex = " + selectedIndex);
        return personProfileArray[selectedIndex];
    }

    function registerClientNotify(notifyCb) {
        notify = notifyCb;
    }

    function getPersonProfiles() {
        return personProfileArray;
    }

    return {
        insertDataIntoCore: insertDataIntoCore,
        getPersonProfileData: getPersonProfileData,
        downloadPersonProfileData: downloadPersonProfileData,
        registerClientNotify: registerClientNotify,
        getPersonProfiles: getPersonProfiles,
        setSelectedData: setSelectedData,
        getSelectedData: getSelectedData,
        uploadPersonProfileData: uploadPersonProfileData
    };
}());